{-include file=$smarty.const.B_DIR|cat:'/src/_header.htm'-}

<script type="text/javascript">
Ext.onReady(function() {
	Ext.QuickTips.init();
	var units	= {-$units-}
	var poultry	= {-$poultry-}

	var _units	= {};
	for(var i in units.root){
		_units[parseInt(units.root[i].id)]	= units.root[i];
	}
	var _poultry	= {};
	for(var i in poultry.root){
		_poultry[parseInt(poultry.root[i].id)]	= poultry.root[i];
	}

	var update	= function(){
		if( grid.getBottomToolbar().disabled){
			return false;
		}
		var o = sm.getSelections();
		if(  o.length!== 1 ){						
			var o = {
				title: "注意" ,
				msg: "请选择一条记录!" ,
				buttons: Ext.Msg.OK,
				icon: Ext.MessageBox.ERROR
			};
			Ext.MessageBox.show(o);
			return ;
		}
		window.setTitle('修改营养素');
		grid.getBottomToolbar().disable();
		window.show();
		form.getForm().setValues(o[0].data);

	}

	var insert = function(){
		grid.getBottomToolbar().disable();
		window. setTitle('添加营养素');
		window.show();
		form.getForm().reset();
		
	}
	var insertOk = function(){
		if( !form.form.isValid()){
			return false;
		}
		form.form.submit({
			method: 'POST',
			waitMsg: '验证中..',
			success: function(form, action) {
				window.hide();
				store.reload({callback: function(){
					window.hide();
					grid.getBottomToolbar().enable();
				}});
			},
			failure: function(form , action){ //()  response.responseText
				alert("递交信息失败，请联系管理员")
			}
		});
	}
	
	var store = new Ext.data.JsonStore({
		url: '?act=list',
		root:'root',
		id:'id',
		fields: [
			{name:'id', type:'int'},
			'营养素名',
			'代码',
			{name:'单位', type:'int'},
			{name:'动物种类', type:'int'},
			'别名',
			'备注'
		]	
	});
	
	var sm = new Ext.grid.CheckboxSelectionModel(); 
	var grid = new Ext.grid.GridPanel({
		sm:sm,
		store: store,
		columns: [
			{id:'id',header: "ID", width: 40, dataIndex: 'id'},
			{header: "营养素名", width: 140, sortable: true, dataIndex: '营养素名'},
			{header: "代码", width: 140, sortable: true,dataIndex: '代码'},
			{header: "单位", width: 80, dataIndex: '单位', renderer: function(val){
				return  _units[val] ? _units[val]['单位'] : '';
			}},
			{header: "动物种类", width: 80, dataIndex: '动物种类', renderer: function(val){
				return  _poultry[val] ? _poultry[val]['动物种类'] : '';
			}},
			{header: "备注", width: 280, dataIndex: '备注'}
		],
		stripeRows: true,
		region:'center',
		bbar:[
			{
				text:'添加',				
				iconCls:'icon-add',
				handler:insert
			}, '-',
			{
				text:'编辑',
				iconCls:'icon-edit',
				handler: update
			}, '-', {
				text:'删除',
				iconCls:'icon-delete',
				handler: function(){
					var o = sm.getSelections();
					if( ! o.length ){						
						var o = {
							title: "注意" ,
							msg: "请至少选择一条记录!" ,
							buttons: Ext.Msg.OK,
							icon: Ext.MessageBox.ERROR
						};
						Ext.MessageBox.show(o);
						return ;
					}
					var r = []
					Ext.each(o,function(node){
						r.push(node.data.id);
					});
					Ext.MessageBox.confirm('提示', '您确定要删除第'+ r.join(',') +'条记录吗?', function(ret){
						if( ret !== 'yes' ){
							return false;
						}
						grid.getBottomToolbar().disable();	
						Ext.Ajax.request({
							url: '?act=del',
							success: function(response, options ){
								if(response.responseText !='ok'){
									Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！", function(){
										alert(response.responseText);
									})
								}else{
									store.reload({callback: function(){
										grid.getBottomToolbar().enable();
									}});
								}
							},
							failure: function(response, options){
								Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！")
							},
							params: { 
								dels : r.join(',')
							}
						});
					});
					//grid.getBottomToolbar().disable();
				}
			}, '->', {
				text: '重载数据',
				iconCls:'icon-refresh',
				handler: function(){
					viewport.disable();
					store.reload({
						callback: function(){
							viewport.enable();
						}
					});
				}
			}
		]
	});

	grid.on({
		'celldblclick':update
	});
	
	var viewport = new Ext.Viewport({
		layout:'border',
		items:grid
	});
	viewport.disable();
	store.load({callback: function(){
		viewport.enable();
	}});
	//===================


	var ds1	= new Ext.data.JsonStore({
			url: '{-$upath-}/units.js',
			root: 'root',
			fields: [
				{name:'id', type:'int'},
				{name: 'txt', mapping:'单位'},
				{name: 'txt2', mapping:'采食量单位'}
			]
		 });
	ds1.load();
	var combo1 = new Ext.form.ComboBox({
		 valueField  :'id',
		displayField:'txt',
		store: ds1,
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		emptyText:'请选择..',
		selectOnFocus:true,
		editable: false, 
		fieldLabel: '单位',
		name: '单位1',
		hiddenName:'单位',
		tpl: '<tpl for="."><div ext:qtip="采食量单位: {txt2}" class="x-combo-list-item">{txt}</div></tpl>',
		anchor:'44%'
	});

	var ds2	= new Ext.data.JsonStore({
			url: '{-$upath-}/poultry.js',
			root: 'root',
			fields: [{name:'id', type:'int'},  {name: 'txt', mapping:'动物种类'}]
		 });
	ds2.load();
	var combo2 = new Ext.form.ComboBox({
		valueField  :'id',
		displayField:'txt',
		store: ds2,
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		emptyText:'请选择..',
		selectOnFocus:true,
		editable: false, 
		fieldLabel: '动物种类',
		name: '动物种类1', hiddenName:'动物种类',
		anchor:'44%'
	});


	//===================
	
	var form = new Ext.form.FormPanel({
		baseCls: 'x-plain',
		labelWidth: 55,
		url:'?act=save',
		defaultType: 'textfield',
		items: [
			{
				xtype:'hidden',
				name:'id'
			},{
				xtype: 'textfield',
				fieldLabel: '营养素名',
				name: '营养素名',
				allowBlank: false,
				minLength:1
			},{
				xtype: 'textfield',
				fieldLabel: '代码',
				name: '代码'
			},
			combo1, combo2
			,{
				xtype: 'textfield',
				fieldLabel: '别名',
				name: '别名',
				anchor: '98%'
			},{
				xtype: 'textarea',
				fieldLabel: '备注',
				name: '备注',
				anchor: '98% -73'  // anchor width by percentage and height by raw adjustment
			}
		]
	});
	
	//var mask	= new Ext.LoadMask( grid.getEl(),{msg:'数据读取中...',removeMask:true});

	var window = new Ext.Window({
		title: '添加动物种类',
		width: 400,
		height:270,
		minWidth: 290,
		minHeight: 100,
		layout: 'fit',
		plain:true,
		bodyStyle:'padding:5px;',
		buttonAlign:'center',
		maskDisabled : false,
		items: form,
		style :'z-index:9999',
		buttons: [
			{
				text: '保存',
				handler: insertOk
			},{
				text: '取消',
				handler: function(){
					window.hide();
					grid.getBottomToolbar().enable();
				}
			}
		]
	});

	window.on({
		 beforeclose  : function(){
			window.hide();
			grid.getBottomToolbar().enable();
			return false;
		}
	});


});
</script>


{-include file=$smarty.const.B_DIR|cat:'/src/_footer.htm'-}